package com.servlet;

import com.JdbcUtils;
import com.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 接收前端提交的参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        // 根据前端的提交的账号和密码，到数据库中查询用户信息
        String sql = "SELECT id, username, `nickname` FROM ec_simple_user WHERE username = ? AND `password` = MD5(?)";
        User user = JdbcUtils.select(sql, User.class, username, password);
        // 只要 user 和 user.id 不是 null 值，视为账号和密码匹配
        boolean isOk = user != null && user.getId() != null;
        PrintWriter writer = response.getWriter();
        if(isOk) {
            writer.write("OK");
        } else {
            writer.write("NO");
        }
        writer.flush();
    }
}
